package com.WaVel.service.impl;

import com.Emplyment.dto.UserRegisterDto;
import com.Emplyment.entity.UserRegister;
import com.Emplyment.exception.RegisterException;
import com.Emplyment.result.Result;
import com.WaVel.mapper.EmplyeeMapper;
import com.WaVel.service.EmplyeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
@Slf4j
public class EmplyeeServiceImpl implements EmplyeeService {

    @Autowired
    private EmplyeeMapper emplyeeMapper;

    /**
     * 注册功能
     * @param username
     * @param password
     */
    @Override
    public void UserRegister(String username, String password) {

        if (username.equals("") || password.equals("")){
            throw new RegisterException("注册异常，请重新注册");
        }
        //MD5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        UserRegister userRegister = emplyeeMapper.selectEmplyemnt(username);
        if (userRegister != null){
            throw new RegisterException("用户名已存在，请重新注册");
        }

        userRegister = UserRegister.builder()
        .username(username)
        .password(password)
        .createTime(LocalDateTime.now())
        .build();

        emplyeeMapper.InsterUser(userRegister);
    }

    /**
     * 用户登录
     * @param username
     * @param password
     */
    @Override
    public UserRegister UserLogin(String username, String password) {
        if (username.equals("") || password.equals("")){
            throw new RegisterException("账号或者密码为空，请重新输入");
        }
        //MD5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        UserRegister userRegister = emplyeeMapper.selectEmplyemnt(username);
        if (userRegister == null){
            throw new RegisterException("账号或者密码错误");
        }
        if (!userRegister.getPassword().equals(password)){
            log.info(userRegister.getPassword());
            log.info(password);
            throw new RegisterException("密码错误，请重新输入");
        }

        return userRegister;

    }
}
